home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12112 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  126 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: How to get at base class fields from member functions?
  5. Message-ID: <marnoldDoGE1H.FMH@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <internews46B00D1FBD@argonet.co.uk> <4ihhkf$2ni@news5.erols.com>
  8. Date: Mon, 18 Mar 1996 07:56:05 GMT
  9. Sender: marnold@netcom4.netcom.com
  10.  
  11. Chris Cobb <ccobb@erols.com> writes:
  12.  
  13. >Dave Mullard <dmullard@argonet.co.uk> wrote:
  14. >>Given three classes A, B and C I want to have multiple As for each B and
  15. >>multiple Bs for each C. To do this I could create the following.
  16. >>
  17. >>class b1 : public B
  18. >>{
  19. >>A a1;
  20. >>A a2;
  21. >>A a3;
  22. >>};
  23. >>
  24. >>class b2 : public B
  25. >>{
  26. >>A a1;
  27. >>A a2;
  28. >>A a3;
  29. >>A a4;
  30. >>A a5;
  31. >>};
  32. >>
  33. >>class c1 : public C
  34. >>{
  35. >>b1 b1;
  36. >>b2 b2;
  37.  
  38. Well, as a side-point, the above variable names are illegal---you can't
  39. name a variable the same name as it's type (as with the two data members
  40. of c1 above---b1 and b2 need to have different names from their types).
  41.  
  42. >>};
  43. >>
  44. >>The question is, how within functions for class B do I access fields
  45. >>within class C, and similarly, how within the class A functions do I
  46. >>access fields within class B?
  47.  
  48.  
  49. You have to use a somewhat inconvenient "forward declaration" syntax
  50. so B can refer to types the compiler hasn't seen yet (namely, class C).  
  51. However, you may only use pointers and references to the forward-
  52. declared types.   
  53.  
  54. C++ is parsed somehwat "linearly"; the compiler does not take 
  55. everything in and then decide if what you coded was legal or not.  
  56. Instead, you must write code with the awareness that, at some points, 
  57. the compiler may not yet understand what you a talking about 
  58. (specifically when it comes to refering to the different classes and 
  59. data types in your program).
  60.  
  61.  
  62. For example, here is how B might access a data member of C (modelled
  63. after your sample code)...
  64.  
  65.  
  66. class C;  // this is a forward declaration to a type not yet seen
  67.  
  68. class B
  69.    {
  70.    public:
  71.       void use_b1_in_C(C& c);  // here we refer to the unseen type C
  72.    };
  73.  
  74. class b1: public B
  75.    {
  76.    public:
  77.       void use_me() { }
  78.    };
  79.  
  80. class C
  81.    {
  82.    public:
  83.       b1 _b1;
  84.    };
  85.  
  86. // now the that compiler has seen C (and b1 too), we can write code 
  87. // for B...
  88.  
  89. void B::use_b1_in_C(C& c)
  90.    {
  91.    c._b1.use_me();
  92.    }
  93.  
  94. // and use it all like this...
  95.  
  96. int main()
  97.    {
  98.    B b;
  99.    C c;
  100.  
  101.    b.use_b1_in_C(c);  // call's b1::use_me() for _b1 member of C
  102.  
  103.    return 0;
  104.    }
  105.  
  106.  
  107. Note that public data members are also required here (or you could 
  108. make B a "friend" of both b1 and C if the data members are kept 
  109. private, as you orignally had them) so B can access them.
  110.  
  111. If you aren't aware, your original code declared all private members, 
  112. since this is the default for a C++ class when you don't specify 
  113. otherwise.  However, this was not the main restriction to doing what
  114. you wanted.  The main problem was being able to write code for B in
  115. terms of C, which requires forward declarations.
  116.  
  117.  
  118. Regards,
  119. -------------------------------------------------------------------------
  120. Matt Arnold                       |        | ||| | |||| |  | | || ||
  121. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  122. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  123. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  124. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  125. -------------------------------------------------------------------------
  126.